#include <Editor/StdAfx.h>
#include "TerrainEditPage.h"
#include "wxIDs.h"
#include "GameToolApp.h"
#include "FileFunctions.h"
#include "../GameLib/EngineFacade.h"
#include "../GameLib/TerrainLayerInfo.h"
#include "../GameLib/FILE_EXTENTIONS.h"
#include "../DebugUtils/DebugUtils.h"

BEGIN_EVENT_TABLE(TerrainEditPage, wxPanel)
	//EVT_BUTTON(ID_CREATE_TERRAIN_BUTTON, OnCreateTerrain)
	EVT_RADIOBUTTON(ID_RADIO_TERRAIN_HEIGHT, OnTerrainHeightRadio)
	EVT_CHECKBOX(ID_CHECKBOX_USE_TERRAIN_NORMALMAP, OnUseNormalMapCheckBox)
	EVT_COMBOBOX(ID_COMBO_TERRAIN_BRUSH, OnTerrainBrushCombo)
	EVT_TEXT(ID_TEXT_BRUSH_SIZE, OnBrushSizeText)
	EVT_TEXT_ENTER(ID_TEXT_BRUSH_SIZE, OnBrushSizeText)
	EVT_TEXT(ID_TEXT_BRUSH_WEIGHT, OnBrushWeightText)
	EVT_TEXT_ENTER(ID_TEXT_BRUSH_WEIGHT, OnBrushWeightText)
	EVT_TEXT(ID_BRUSH_CONSTANT_HEIGHT, OnBrushConstantHeightText)
	EVT_TEXT_ENTER(ID_BRUSH_CONSTANT_HEIGHT, OnBrushConstantHeightText)
	EVT_BUTTON(ID_BUTTON_ADD_LAYER, OnAddLayer)
END_EVENT_TABLE()

TerrainEditPage::TerrainEditPage(wxWindow* parent)
	: wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxVSCROLL)
{
	mLayerSizer = 0;
	mTerrainCreated = false;
	mEditingMode = TEM_HEIGHT;
	mEditingBlendMapIndex = 1;
	wxBoxSizer* topSizer = new wxBoxSizer( wxVERTICAL);
	SetSizer(topSizer);
	

	// BRUSH START ----
		wxStaticBoxSizer* brushSizer = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, _("Brush")), wxVERTICAL);
		topSizer->Add(brushSizer, wxSizerFlags().Expand().Border());
		// Brushes
		brushSizer->Add(new wxStaticText(this, wxID_ANY, _("Select brush:")), wxSizerFlags().Border());
		wxArrayString terrainBrushStrings;
		for (unsigned i=0; i< TBM_COUNT; i++)
		{
			terrainBrushStrings.Add(wxString::FromAscii(gTerrainBrushStrings[i]));
		}

		brushSizer->Add(new wxComboBox(this, ID_COMBO_TERRAIN_BRUSH, *terrainBrushStrings.begin(), wxDefaultPosition, wxDefaultSize, terrainBrushStrings), 
			wxSizerFlags().Border().Expand());

		// Brush Size
		wxBoxSizer* brushSizeSizer = new wxBoxSizer(wxHORIZONTAL);
		brushSizer->Add( brushSizeSizer );
		brushSizeSizer->Add( new wxStaticText(this, wxID_ANY, _("Brush Size:")), wxSizerFlags().Border());
		brushSizeSizer->Add( new wxTextCtrl(this, ID_TEXT_BRUSH_SIZE, wxT("1"), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER),
			wxSizerFlags().Border());
		brushSizeSizer->Add( new wxStaticText(this, wxID_ANY, _("meter")), wxSizerFlags().Border());

		// Brush Weight
		wxBoxSizer* brushWeightSizer = new wxBoxSizer(wxHORIZONTAL);
		brushSizer->Add( brushWeightSizer );
		brushWeightSizer->Add( new wxStaticText(this, wxID_ANY, _("Brush Weight:")), wxSizerFlags().Border());
		brushWeightSizer->Add( new wxTextCtrl(this, ID_TEXT_BRUSH_WEIGHT, wxT("1"), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER),
			wxSizerFlags().Border());

		// Brush Constant Height
		wxBoxSizer* brushConstantSizer = new wxBoxSizer(wxHORIZONTAL);
		brushSizer->Add( brushConstantSizer );
		brushConstantSizer->Add( new wxStaticText(this, wxID_ANY, _("Constant Height:")), wxSizerFlags().Border());
		brushConstantSizer->Add( new wxTextCtrl(this, ID_BRUSH_CONSTANT_HEIGHT, wxT("0"), wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER),
			wxSizerFlags().Border());
		brushConstantSizer->Add( new wxStaticText(this, wxID_ANY, _("meter")), wxSizerFlags().Border());
	// --- BRUSH END

	// HEIGHT EDIT START ---
		wxStaticBoxSizer* heightEditSizer = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, _("Edit Height")), wxVERTICAL);
		topSizer->Add(heightEditSizer, wxSizerFlags().Expand().Border());
		// Height Edit Radio Button
		heightEditSizer->Add(new wxRadioButton(this, ID_RADIO_TERRAIN_HEIGHT, _("Edit Height"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP), 
			wxSizerFlags());

		wxBoxSizer* directionSizer =new wxBoxSizer(wxHORIZONTAL);
		heightEditSizer->Add(directionSizer);
	// --- HEIGHT EDIT END

	//Layer
	PopulateLayerWidgets();

	topSizer->SetSizeHints(this);
	SetScrollRate(0, 10);
}


void TerrainEditPage::OnCreateTerrain()
{
	mTerrainCreated = true;
	wxGetApp().SetTerrainEditMode(mEditingMode, mEditingBlendMapIndex);
	wxGetApp().SetTerrainHeightBrush(gTerrainBrushStrings[0]);
	PopulateLayerWidgets();		

	wxCheckBox* normalmapCheckbox = dynamic_cast<wxCheckBox*>(FindWindow(ID_CHECKBOX_USE_TERRAIN_NORMALMAP));
	assert(normalmapCheckbox);
	normalmapCheckbox->SetValue(false);
}

void TerrainEditPage::OnLoadTerrain()
{
	mTerrainCreated = true;
	wxGetApp().SetTerrainEditMode(mEditingMode, mEditingBlendMapIndex);
	wxGetApp().SetTerrainHeightBrush(gTerrainBrushStrings[0]);
	PopulateLayerWidgets();
}

void TerrainEditPage::OnSaveTerrain(wxCommandEvent& e)
{
	/*
	if (mFilename.empty())
	{
		wxTextEntryDialog dialog(this, _("Enter terrain file name without extension. ex) MyFirstTerrain"), _("Save"));
		if (dialog.ShowModal() == wxID_OK)
		{
			wxFileName inputFilename = dialog.GetValue();
			wxString filename = inputFilename.GetName();
			if ( !filename.empty() )
			{
				mFilename = filename + TERRAIN_EXT;
				bool exists = EngineFacade::TerrainExists(mFilename.c_str());
				if (exists)
				{
					wxMessageDialog confirmOverwrite(this, _("Do you want to overwrite existing file?"), _("Warning"), wxYES_NO|wxNO_DEFAULT);
					if (confirmOverwrite.ShowModal() != wxID_YES)
					{
						return;
					}
				}
			}
			else
			{
				wxGetApp().Message(_("Emtpy terrain file name is not allowed."));
				return;
			}
		}
		else
		{
			return;
		}
	}
	assert(!mFilename.empty());
	wxGetApp().SaveTerrain(mFilename.c_str());
	*/
}

void TerrainEditPage::OnTerrainHeightRadio(wxCommandEvent& e)
{
	wxRadioButton* pRadioButton = dynamic_cast<wxRadioButton*>(e.GetEventObject());
	if (pRadioButton)
	{
		if (pRadioButton->GetValue())
		{
			mEditingMode = TEM_HEIGHT;
			wxGetApp().SetTerrainEditMode(mEditingMode);
		}
	}
}

void TerrainEditPage::OnUseNormalMapCheckBox(wxCommandEvent& e)
{
	wxCheckBox* checkbox = dynamic_cast<wxCheckBox*>(e.GetEventObject());
	if (checkbox)
	{
		bool use = checkbox->GetValue();
		wxGetApp().UseTerrainNormalHeightMap(use);
	}
}

void TerrainEditPage::OnTerrainBrushCombo(wxCommandEvent& e)
{
	wxComboBox* pComboBox = dynamic_cast<wxComboBox*>(e.GetEventObject());
	if (pComboBox)
	{
		wxString newValue = pComboBox->GetValue();
		wxGetApp().SetTerrainHeightBrush(newValue.mb_str());
	}
}

void TerrainEditPage::OnBrushSizeText(wxCommandEvent& e)
{
	if (e.GetEventType() == wxEVT_COMMAND_TEXT_ENTER)
	{
		wxGetApp().SetFocusToMainViewPanel();
	}
	wxTextCtrl* pText = dynamic_cast<wxTextCtrl*>(e.GetEventObject());
	if (pText)
	{		
		double size = 0;
		bool success = pText->GetValue().ToDouble(&size);
		if (success)
		{
			float sizeInMeter = (float)size;
			wxGetApp().SetTerrainBrushSize(sizeInMeter);
		}
	}
}

void TerrainEditPage::OnBrushWeightText(wxCommandEvent& e)
{
	if (e.GetEventType() == wxEVT_COMMAND_TEXT_ENTER)
	{
		wxGetApp().SetFocusToMainViewPanel();
	}
	wxTextCtrl* pText = dynamic_cast<wxTextCtrl*>(e.GetEventObject());
	if (pText)
	{		
		double weight = 0;
		bool success = pText->GetValue().ToDouble(&weight);
		if (success)
		{
			wxGetApp().SetTerrainBrushWeight(weight);
		}
	}
}

void TerrainEditPage::OnBrushConstantHeightText(wxCommandEvent& e)
{
	wxTextCtrl* pText = dynamic_cast<wxTextCtrl*>(e.GetEventObject());
	if (pText)
	{
		
		double size = 0;
		bool success = pText->GetValue().ToDouble(&size);
		if (success)
		{
			float sizeInMeter = (float)size;
			wxGetApp().SetTerrainBrushConstantHeight(sizeInMeter);
		}
	}
}

void TerrainEditPage::OnAddLayer(wxCommandEvent& e)
{
	if (mTerrainCreated)
	{
		unsigned layerCount = wxGetApp().AddTerrainLayer();
		bool success = wxGetApp().GetTerrainLayerInfo(mTerrainLayerInfo);
		if (success)
		{
			unsigned size = mTerrainLayerInfo.GetSize();
			assert(layerCount == size);
			mLayerSizer->Detach(mAddLayerButton);
			AddLayerWidgets(layerCount-1);
			mLayerSizer->Add(mAddLayerButton, wxSizerFlags().Border());
			//Layout();
			FitInside();
		}
	}
	else
	{
		DebugUtils::Warning(DEFAULT_DEBUG_ARG, "Terrain is not created yet.");
	}

}

void TerrainEditPage::OnSetFocusToTextureText(wxFocusEvent& e)
{
	DebugUtils::Warning(DEFAULT_DEBUG_ARG, "focus event");
	wxWindow* window = dynamic_cast<wxWindow*>(e.GetEventObject());
	if (window == NULL)
		return;
	window->Disconnect(e.GetId(), wxEVT_SET_FOCUS);
	bool diffuse_specular = e.GetId() >= ID_TEXT_TERRAIN_DIFFUSE_SPECULAR && e.GetId() < ID_TEXT_TERRAIN_DIFFUSE_SPECULAR+50;
	bool normal_height = e.GetId() >= ID_TEXT_TERRAIN_NORMAL_HEIGHT && e.GetId() < ID_TEXT_TERRAIN_NORMAL_HEIGHT+50;
	if(diffuse_specular || normal_height)
	{
		wxString strFileFilter = 
			_( 
				"Texture File(*.bmp;*.jpg;*.png;*.dds;*.tga)|*.bmp;*.jpg;*.png;*.dds;*.tga|\
				All files(*.*)|*.*"
			);

		wxString defaultDirectory = wxT("Data/terrains/");
		if (!::wxDirExists(defaultDirectory.c_str()))
		{
			defaultDirectory.Clear();
		}
		std::auto_ptr<wxFileDialog> pOpenFileDialog
			( 
				new wxFileDialog
					( 
						this, _("Choose a texture..."), defaultDirectory, wxT(""),
						strFileFilter, 	wxFD_OPEN  | wxFD_FILE_MUST_EXIST
					)
			);
 
		wxString path;
		if ( pOpenFileDialog->ShowModal() == wxID_OK )
		{
			path = pOpenFileDialog->GetPath();
			CopyTargetToDataFolder(path, _("terrains"), this);

			wxFileName imageFilename(path);
			if (diffuse_specular)
			{
				unsigned index = e.GetId() - ID_TEXT_TERRAIN_DIFFUSE_SPECULAR;
				wxGetApp().SetDiffuseSpecularMap(index, imageFilename.GetFullName().mb_str());
			}
			else if (normal_height)
			{
				unsigned index = e.GetId() - ID_TEXT_TERRAIN_NORMAL_HEIGHT;
				wxGetApp().SetNormalHeightMap(index, imageFilename.GetFullName().mb_str());
			}

			wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>(window);
			if (textCtrl)
			{
				textCtrl->ChangeValue(imageFilename.GetFullName().c_str());
			}
			else
			{
				assert(0); // dynamic_cast failed.
			}
		}		
	}

	wxGetApp().SetFocusToMainViewPanel();
	window->Connect(e.GetId(), wxEVT_SET_FOCUS, wxFocusEventHandler(TerrainEditPage::OnSetFocusToTextureText), 0, this);
}

void TerrainEditPage::OnDeleteTextureButton(wxCommandEvent& e)
{
	DebugUtils::Warning(DEFAULT_DEBUG_ARG, "Removing Texture");
	wxWindow* window = dynamic_cast<wxWindow*>(e.GetEventObject());
	if (window == NULL)
		return;
	
	bool diffuse_specular = e.GetId() >= ID_BUTTON_TERRAIN_DIFFUSE_SPECULAR_DELETE && e.GetId() < ID_BUTTON_TERRAIN_DIFFUSE_SPECULAR_DELETE+50;
	bool normal_height = e.GetId() >= ID_BUTTON_TERRAIN_NORAML_HEIGHT_DELETE && e.GetId() < ID_BUTTON_TERRAIN_NORAML_HEIGHT_DELETE+50;
	unsigned index;
	if(diffuse_specular)
	{
		index = e.GetId() - ID_BUTTON_TERRAIN_DIFFUSE_SPECULAR_DELETE;
		wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_TEXT_TERRAIN_DIFFUSE_SPECULAR + index));
		assert(textCtrl);
		textCtrl->Clear();
		wxGetApp().SetDiffuseSpecularMap(index, "");
	}
	else if (normal_height)
	{
		index = e.GetId() - ID_BUTTON_TERRAIN_NORAML_HEIGHT_DELETE;
		wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>(FindWindow(ID_TEXT_TERRAIN_NORMAL_HEIGHT + index));
		assert(textCtrl);
		textCtrl->Clear();
		wxGetApp().SetNormalHeightMap(index, "");
	}
	else
	{
		assert(0);
		DebugUtils::Warning(DEFAULT_DEBUG_ARG, "No valid path for deciding index variable.");
		return;
	}	
}

void TerrainEditPage::OnLayerWorldSizeText(wxCommandEvent& e)
{
	unsigned index = e.GetId() - ID_TEXT_TERRAIN_LAYER_WORLD_SIZE;
	wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>(e.GetEventObject());
	if (textCtrl)
	{
		double worldSize;
		bool success = textCtrl->GetValue().ToDouble(&worldSize);
		if (success)
		{
			wxGetApp().SetLayerWorldSize(index, (float)worldSize);
		}
	}
	else
	{
		assert(0); // dynamic_cast failed.
	}
}

void TerrainEditPage::OnLayerWorldSizeText_EnterPressed(wxCommandEvent& e)
{
	unsigned index = e.GetId() - ID_TEXT_TERRAIN_LAYER_WORLD_SIZE;
	wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>(e.GetEventObject());
	if (textCtrl)
	{
		double worldSize;
		bool success = textCtrl->GetValue().ToDouble(&worldSize);
		if (success)
		{
			wxGetApp().SetLayerWorldSize(index, (float)worldSize);
			wxGetApp().SetFocusToMainViewPanel();
		}
	}
	else
	{
		assert(0); // dynamic_cast failed.
	}
}

void TerrainEditPage::OnEditBlend(wxCommandEvent& e)
{
	mEditingBlendMapIndex = e.GetId() - ID_RADIO_TERRAIN_BLEND;
	assert(mEditingBlendMapIndex>0 && mEditingBlendMapIndex<=50);

	mEditingMode = TEM_BLEND;
	wxGetApp().SetTerrainEditMode(mEditingMode, mEditingBlendMapIndex);
}

void TerrainEditPage::OnDeleteLayerButton(wxCommandEvent& e)
{
	unsigned index = e.GetId() - ID_BUTTON_DELETE_LAYER;
	assert(index<256);

	if (index == mEditingBlendMapIndex)
	{
		mEditingBlendMapIndex = 1;
		mEditingMode=TEM_HEIGHT;
		wxGetApp().SetTerrainEditMode(mEditingMode, mEditingBlendMapIndex);
		wxRadioButton* radio = dynamic_cast<wxRadioButton*>(FindWindow(ID_RADIO_TERRAIN_HEIGHT));
		if (radio)
		{
			radio->SetValue(true);
		}
	}
	bool success = wxGetApp().RemoveTerrainLayer(index);
	if (success)
	{
		PopulateLayerWidgets();
	}
}

bool TerrainEditPage::Show(bool show)
{
	if (show)
	{
		wxGetApp().SetTerrainEditMode(mEditingMode, mEditingBlendMapIndex);
	}
	else
	{
		wxGetApp().SetTerrainEditMode(TEM_NO_EDIT);
	}
	return wxPanel::Show(show);
}

void TerrainEditPage::PopulateLayerWidgets()
{
	if (mLayerSizer)
	{
		mLayerSizer->Clear(true);
	}
	else
	{
		mLayerSizer = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, _("Edit Layer")), wxVERTICAL);
		GetSizer()->Add(mLayerSizer, wxSizerFlags().Expand().Border());
	}
	
	mLayerSizer->Add(new wxCheckBox(this, ID_CHECKBOX_USE_TERRAIN_NORMALMAP, _("Use NormalHeightMap")), wxSizerFlags().Border());

	mAddLayerButton = new wxButton(this, ID_BUTTON_ADD_LAYER, _("Add Layer"));
	mAddLayerButton->Hide();

	if (mTerrainCreated)
	{
		bool success = wxGetApp().GetTerrainLayerInfo(mTerrainLayerInfo);
		if (!success)
		{
			DebugUtils::Warning(DEFAULT_DEBUG_ARG, "GetTerrainLayerInfo failed.");
			return;
		}

		unsigned size = mTerrainLayerInfo.GetSize();
		for (unsigned i=0; i<size; i++)
		{
			AddLayerWidgets(i);
		}
		mLayerSizer->Add( mAddLayerButton, wxSizerFlags().Border());
		mAddLayerButton->Show();
	}
	mLayerSizer->FitInside(this);
	Layout();
	FitInside();
}

void TerrainEditPage::AddLayerWidgets(unsigned index)
{
	assert(index < mTerrainLayerInfo.GetSize());

	TerrainLayer& layer = mTerrainLayerInfo.mLayers[index];
	wxString staticBoxLabel = wxString::Format(_("Layer %d"), index);
	wxStaticBoxSizer* groupSizer = new wxStaticBoxSizer( new wxStaticBox(this, wxID_ANY, staticBoxLabel), wxVERTICAL );
	mLayerSizer->Add(groupSizer, wxSizerFlags().Expand().Border(20));

	// diffuse specular
	wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
	groupSizer->Add(sizer, wxSizerFlags().Expand().Border(20));
	sizer->Add(new wxStaticText(this, wxID_ANY, _("Diffuse(RGB)\nSpecular(A) Map: ")), wxSizerFlags().Border(20));
	wxTextCtrl* diffuseSpecularText = new wxTextCtrl(this, ID_TEXT_TERRAIN_DIFFUSE_SPECULAR+index, wxString::FromAscii(layer.mDiffuseSpecular.c_str()));
	sizer->Add( diffuseSpecularText, wxSizerFlags(1).Border(20));
	diffuseSpecularText->Connect(ID_TEXT_TERRAIN_DIFFUSE_SPECULAR+index, wxEVT_SET_FOCUS, wxFocusEventHandler(TerrainEditPage::OnSetFocusToTextureText), 0, this);
	wxButton* diffuseDeleteButtton = new wxButton(this, ID_BUTTON_TERRAIN_DIFFUSE_SPECULAR_DELETE+index, wxT("X"), wxDefaultPosition, wxSize(10, 10));
	sizer->Add(diffuseDeleteButtton, wxSizerFlags());
	diffuseDeleteButtton->Connect(ID_BUTTON_TERRAIN_DIFFUSE_SPECULAR_DELETE+index, wxEVT_COMMAND_BUTTON_CLICKED, 
		wxCommandEventHandler(TerrainEditPage::OnDeleteTextureButton), 0, this);

	// normal height
	sizer = new wxBoxSizer(wxHORIZONTAL);
	groupSizer->Add(sizer, wxSizerFlags().Expand().Border(20));
	sizer->Add(new wxStaticText(this, wxID_ANY, _("Normal(RGB)\nHeight(A) Map: ")), wxSizerFlags().Border(20));
	wxTextCtrl* normalHeightText = new wxTextCtrl(this, ID_TEXT_TERRAIN_NORMAL_HEIGHT+index, wxString::FromAscii(layer.mNormalHeight.c_str()));
	sizer->Add( normalHeightText, wxSizerFlags(1).Border(20));
	normalHeightText->Connect(ID_TEXT_TERRAIN_NORMAL_HEIGHT+index, wxEVT_SET_FOCUS, wxFocusEventHandler(TerrainEditPage::OnSetFocusToTextureText), 0, this);
	wxButton* normalDeleteButton = new wxButton(this, ID_BUTTON_TERRAIN_NORAML_HEIGHT_DELETE+index, wxT("X"), wxDefaultPosition, wxSize(10, 10));
	sizer->Add(normalDeleteButton, wxSizerFlags());
	normalDeleteButton->Connect(ID_BUTTON_TERRAIN_NORAML_HEIGHT_DELETE+index, wxEVT_COMMAND_BUTTON_CLICKED, 
		wxCommandEventHandler(TerrainEditPage::OnDeleteTextureButton), 0, this);

	// world size
	sizer = new wxBoxSizer(wxHORIZONTAL);
	groupSizer->Add(sizer, wxSizerFlags().Expand().Border(20));
	sizer->Add(new wxStaticText(this, wxID_ANY, _("World Size: ")), wxSizerFlags().Border(20));
	wxString strWorldSize = wxString::Format(wxT("%.2f"), layer.mWorldSize);
	wxTextCtrl* worldSizeText = new wxTextCtrl(this, ID_TEXT_TERRAIN_LAYER_WORLD_SIZE+index, strWorldSize, wxDefaultPosition, wxSize(50, 20), wxTE_PROCESS_ENTER);
	sizer->Add(worldSizeText, wxSizerFlags().Border(20));
	sizer->Add(new wxStaticText(this, wxID_ANY, _("meter")), wxSizerFlags().Border(20));
	worldSizeText->Connect(ID_TEXT_TERRAIN_LAYER_WORLD_SIZE+index, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler(TerrainEditPage::OnLayerWorldSizeText), 0, this);
	worldSizeText->Connect(ID_TEXT_TERRAIN_LAYER_WORLD_SIZE+index, wxEVT_COMMAND_TEXT_ENTER, 
				wxCommandEventHandler(TerrainEditPage::OnLayerWorldSizeText_EnterPressed), 0, this);

	if (index !=0)
	{
		wxBoxSizer* blendSizer = new wxBoxSizer(wxHORIZONTAL);
		groupSizer->Add(blendSizer, wxSizerFlags(1).Expand());
		wxString label = wxString::Format(_("Edit Blend Map %d"), index);
		wxRadioButton* blendRadio = new wxRadioButton(this, ID_RADIO_TERRAIN_BLEND+index, label);
		blendSizer->Add(blendRadio, wxSizerFlags(1).Border().Expand());
		blendRadio->Connect(ID_RADIO_TERRAIN_BLEND+index, wxEVT_COMMAND_RADIOBUTTON_SELECTED, wxCommandEventHandler(TerrainEditPage::OnEditBlend), 0, this);

		wxButton* deleteButton = new wxButton(this, ID_BUTTON_DELETE_LAYER+index, _("Delete"), wxDefaultPosition, wxSize(45, 18));
		blendSizer->Add(deleteButton, wxSizerFlags().Border().Right());
		deleteButton->Connect(ID_BUTTON_DELETE_LAYER+index, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(TerrainEditPage::OnDeleteLayerButton), 0, this);
	}
}